package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbMinuteFileBo;
import org.dromara.system.domain.vo.WbMinuteFileVo;
import org.dromara.system.domain.WbMinuteFile;
import org.dromara.system.mapper.WbMinuteFileMapper;
import org.dromara.system.service.IWbMinuteFileService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 中标结果附件Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
public class WbMinuteFileServiceImpl implements IWbMinuteFileService {

    private final WbMinuteFileMapper baseMapper;

    /**
     * 查询中标结果附件
     */
    @Override
    public WbMinuteFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询中标结果附件列表
     */
    @Override
    public TableDataInfo<WbMinuteFileVo> queryPageList(WbMinuteFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbMinuteFile> lqw = buildQueryWrapper(bo);
        Page<WbMinuteFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询中标结果附件列表
     */
    @Override
    public List<WbMinuteFileVo> queryList(WbMinuteFileBo bo) {
        LambdaQueryWrapper<WbMinuteFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbMinuteFile> buildQueryWrapper(WbMinuteFileBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbMinuteFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWbMinuteBidBagId() != null, WbMinuteFile::getWbMinuteBidBagId, bo.getWbMinuteBidBagId());
        lqw.like(StringUtils.isNotBlank(bo.getFileOriginalName()), WbMinuteFile::getFileOriginalName, bo.getFileOriginalName());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), WbMinuteFile::getFileName, bo.getFileName());
        lqw.eq(bo.getFileType() != null, WbMinuteFile::getFileType, bo.getFileType());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), WbMinuteFile::getUrl, bo.getUrl());
        lqw.eq(bo.getFileSize() != null, WbMinuteFile::getFileSize, bo.getFileSize());
        return lqw;
    }

    /**
     * 新增中标结果附件
     */
    @Override
    public Boolean insertByBo(WbMinuteFileBo bo) {
        WbMinuteFile add = MapstructUtils.convert(bo, WbMinuteFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改中标结果附件
     */
    @Override
    public Boolean updateByBo(WbMinuteFileBo bo) {
        WbMinuteFile update = MapstructUtils.convert(bo, WbMinuteFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbMinuteFile entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除中标结果附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
